home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 5791 / 5791.xpi / chrome / flagfox.jar / content / util.js < prev   
Text File  |  2009-05-21  |  7KB  |  149 lines

  1. function Flagfox_getVersion()
  2. {
  3.     return Components.classes["@mozilla.org/extensions/manager;1"]
  4.                      .getService(Components.interfaces.nsIExtensionManager)
  5.                      .getItemForID("{1018e4d6-728f-4b20-ad56-37578a4de76b}")
  6.                      .version;
  7. }
  8.  
  9. function Flagfox_getLocale()
  10. {
  11.     const prefs = Components.classes["@mozilla.org/preferences-service;1"]
  12.                             .getService(Components.interfaces.nsIPrefBranch);
  13.     try { return prefs.getComplexValue("general.useragent.locale",Components.interfaces.nsIPrefLocalizedString).data; }
  14.     catch (e) { return prefs.getCharPref("general.useragent.locale"); }
  15. }
  16.  
  17. function Flagfox_getWikipediaSearchURL(search)  // Returns a Wikipedia search URL for the current locale
  18. {
  19.     // We do a search here, just in case the exact page doesn't exist or there is no localized version
  20.     // The "variant" option is primarily for zh-CN and zh-TW; it is ignored if there is no variant
  21.     const fulllocale = Flagfox_getLocale().toLowerCase();  // Strangely, Wikipedia "variant" is case-sensitive
  22.     const baselocale = fulllocale.split('-')[0];           // language-dialect --> language
  23.     return "http://" + baselocale + ".wikipedia.org/wiki/Special:Search?search=" + encodeURIComponent(search) + "&go=Go&variant=" + fulllocale;
  24. }
  25.  
  26. function Flagfox_parseException(e)  // Returns a string version of an exception object with its stack trace
  27. {
  28.     if (!e)
  29.         return "";
  30.     else if (!e.stack)
  31.         return String(e);
  32.     else
  33.         return String(e) + " \n" + String(e.stack);
  34. }
  35.  
  36. function Flagfox_error(message, exception)
  37. {
  38.     if (!message)
  39.         message = "Unknown error!";
  40.  
  41.     // Set "javascript.options.showInConsole" to true to view
  42.     Components.utils.reportError("Flagfox ERROR: " + message + " \n" + Flagfox_parseException(exception));
  43.  
  44.     try
  45.     {
  46.         var appInfo = Components.classes["@mozilla.org/xre/app-info;1"]
  47.                                 .getService(Components.interfaces.nsIXULAppInfo)
  48.                                 .QueryInterface(Components.interfaces.nsIXULRuntime);
  49.  
  50.         // No L10N: We only speak English (well) and thus our forums and the problems reported on them need to be in English. Sorry.
  51.         var outputMsg = "Sorry, Flagfox has encountered a problem.  " +
  52.                         "Please copy the report below and paste it on our forums with " +
  53.                         "steps to reproduce so we can attempt to fix your issue.  (English only)\n";
  54.  
  55.         outputMsg += "\nFlagfox version " + Flagfox_getVersion() + "\n";
  56.         outputMsg += "\nERROR MESSAGE: " + message + "\n";
  57.         if (exception)
  58.         {
  59.             outputMsg += "\nEXCEPTION THROWN: " + exception + "\n";
  60.             if (exception.stack)
  61.                 outputMsg += "\nSTACK TRACE: " + exception.stack;  // ends with "\n"
  62.         }
  63.         outputMsg += "\nOPTIONS: " + Flagfox_dumpPrefs() + "\n";
  64.         outputMsg += "\nBROWSER: " + appInfo.vendor + " " + appInfo.name +  " " + appInfo.version + "/" + appInfo.appBuildID
  65.                                    + " (Gecko " + appInfo.platformVersion + "/" + appInfo.platformBuildID + ")"
  66.                                    + " using locale " + Flagfox_getLocale()
  67.                                    + " on " + appInfo.OS + " " + appInfo.XPCOMABI + "\n";
  68.  
  69.         const prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
  70.                                   .getService(Components.interfaces.nsIPromptService);
  71.         var flags = prompts.BUTTON_POS_0 * prompts.BUTTON_TITLE_OK +
  72.                     prompts.BUTTON_POS_1 * prompts.BUTTON_TITLE_IS_STRING +
  73.                     prompts.BUTTON_POS_0_DEFAULT;
  74.         var button = prompts.confirmEx( null, "Flagfox Error!", outputMsg, flags, "", "Forums", "", null, {} );
  75.  
  76.         if (button == 1)  // "Forums" button
  77.         {
  78.             // Open forum in new tab (can't open new window; if error is on startup, we could hit another error)
  79.             Flagfox_addTabInCurrentBrowser("http://flagfox.net/reportingbugs");
  80.         }
  81.     }
  82.     catch (e) { Components.utils.reportError("EXCEPTION DURING FLAGFOX ERROR REPORTING: " + Flagfox_parseException(e)); }
  83. }
  84.  
  85. function Flagfox_addTabInCurrentBrowser(url)
  86. {
  87.     // Find the browser for the most recent window, regardless of where this function was called from
  88.     const currentBrowser = Components.classes["@mozilla.org/appshell/window-mediator;1"]
  89.                                      .getService(Components.interfaces.nsIWindowMediator)
  90.                                      .getMostRecentWindow("navigator:browser")
  91.                                      .getBrowser();
  92.     currentBrowser.selectedTab = currentBrowser.addTab(url,null,null);
  93. }
  94.  
  95. function Flagfox_dumpPrefs()
  96. {
  97.     try
  98.     {
  99.         var branch = Components.classes["@mozilla.org/preferences-service;1"]
  100.                                .getService(Components.interfaces.nsIPrefService)
  101.                                .getBranch("flagfox.");
  102.         var output = "";
  103.         var prefList = branch.getChildList("",{});
  104.         prefList.sort();
  105.         for (var i in prefList)
  106.             output += prefList[i] + "=" + Flagfox_getPref(branch,prefList[i]) + "; ";
  107.         return (output.length ? output : "(MISSING!)");
  108.     }
  109.     catch (e) { return "EXCEPTION on pref dump: " + Flagfox_parseException(e); }
  110. }
  111.  
  112. function Flagfox_getPref(prefs,prefName)
  113. {
  114.     switch (prefs.getPrefType(prefName))
  115.     {
  116.         case 0: return "(INVALID!)";                           // PREF_INVALID
  117.         case 32: return Flagfox_getUCharPref(prefs,prefName);  // PREF_STRING
  118.         case 64: return prefs.getIntPref(prefName);            // PREF_INT
  119.         case 128: return prefs.getBoolPref(prefName);          // PREF_BOOL
  120.     }
  121.     throw "Bad pref type for: " + prefName;
  122. }
  123.  
  124. function Flagfox_getUCharPref(prefs,prefName)  // Unicode getCharPref
  125. {
  126.     return prefs.getComplexValue(prefName, Components.interfaces.nsISupportsString).data;
  127. }
  128.  
  129. function Flagfox_setUCharPref(prefs,prefName,text)  // Unicode setCharPref
  130. {
  131.     var string = Components.classes["@mozilla.org/supports-string;1"]
  132.                            .createInstance(Components.interfaces.nsISupportsString);
  133.     string.data = text;
  134.     prefs.setComplexValue(prefName, Components.interfaces.nsISupportsString, string);
  135. }
  136.  
  137. function Flagfox_hashString(string)  // Returns a base-64 encoded MD5 hash of a Unicode string
  138. {
  139.     var converter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"]
  140.                               .createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
  141.     converter.charset = "UTF-8";
  142.     var bytes = converter.convertToByteArray(string,{});
  143.     var cryptoHash = Components.classes["@mozilla.org/security/hash;1"]
  144.                                .createInstance(Components.interfaces.nsICryptoHash);
  145.     cryptoHash.init(cryptoHash.MD5);
  146.     cryptoHash.update(bytes,bytes.length);
  147.     return cryptoHash.finish(true);
  148. }
  149.